package com.example.sherlockzhong.plush_v2.CardIni;

/*
 * Created by DosonLeung on 2014/8/8.
 */

import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sherlockzhong.plush_v2.CardBeans.ExamCardBean;
import com.example.sherlockzhong.plush_v2.MainActivity;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Utils.DisplayUtil;
import com.example.sherlockzhong.plush_v2.Utils.ShortIDUtil;
import com.example.sherlockzhong.plush_v2.View.CardHorizontalScrollView;
import com.example.sherlockzhong.plush_v2.View.MainContentFrame;

public class ExamCardIni {
    public static void generateExamCard(MainContentFrame container, int cardId, MainActivity app, Context ctx, ExamCardBean ECB) {
        String cardID;
        String cardClass;
        //the default scale of the cards
        int marginLeftRight = 20;
        int marginTopBottom = 20;
        int cardwidth = 480;
        int cardheight = 296;
        int exam_title_size = 35;
        int exam_othertext_size = 26;

        //get the width of screen
        Display display = app.getWindowManager().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        final int windowWidth = p.x;

        //the responsive scale of the card
        cardwidth = windowWidth - marginLeftRight * 2;
        cardheight = (int) ((float) cardwidth * 0.55);

        exam_title_size = DisplayUtil.px2sp(ctx, (int) ((float) cardwidth * 0.065));
        exam_othertext_size = DisplayUtil.px2sp(ctx, (int) ((float) cardwidth * 0.045));

        final CardHorizontalScrollView cardScroller = (CardHorizontalScrollView) app.getLayoutInflater().inflate(R.layout.card_container, container, false);
        LinearLayout cardContainer = (LinearLayout) cardScroller.findViewById(R.id.card_container);
        View card = app.getLayoutInflater().inflate(cardId, cardContainer, false);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(cardwidth, cardheight, 1);
        params.setMargins(windowWidth + marginLeftRight, marginTopBottom, windowWidth + marginLeftRight, 0);

        TextView exam_title = (TextView) card.findViewById(R.id.exam_title);
        exam_title.setTextSize(exam_title_size);
        TextView exam_address = (TextView) card.findViewById(R.id.exam_address);
        exam_address.setTextSize(exam_othertext_size);
        TextView exam_date = (TextView) card.findViewById(R.id.exam_date);
        exam_date.setTextSize(exam_othertext_size);
        TextView exam_time = (TextView) card.findViewById(R.id.exam_time);
        exam_time.setTextSize(exam_othertext_size);
        TextView exam_deadline = (TextView) card.findViewById(R.id.exam_deadline);
        exam_deadline.setTextSize(exam_othertext_size);
        card.setLayoutParams(params);

        cardContainer.addView(card);


        HorizontalScrollView.LayoutParams params2 = new HorizontalScrollView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, cardheight+marginTopBottom);

        /*cardScroller setting
        * set cardScroller width
        * set cardScroller
        * */
        cardScroller.setLayoutParams(params2);
        cardScroller.setScrollDistance(windowWidth);
        cardScroller.setApp(app);
        cardScroller.setCtx(ctx);
        cardScroller.setRootConatiner(container);
        cardScroller.post(new Runnable() {
            @Override
            public void run() {
                cardScroller.scrollTo(windowWidth, 0);
            }
        });

        cardID = ShortIDUtil.generateShortUuid();
        cardClass = ExamCardBean.getCardClass();
        cardScroller.setCardId(cardID);
        cardScroller.setCardClass(cardClass);

        //cardScroller.setIndex(container.getCardIndexByCardId(cardID));
        container.addCard(app, cardScroller);
        ECB.insertTo(cardScroller);
    }

}

